package com.example.exceldemo.liteflowComponent.gs;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.RandomUtil;
import com.deepoove.poi.config.Configure;
import com.deepoove.poi.plugin.highlight.HighlightRenderPolicy;
import com.example.exceldemo.constants.ReqMgrConstants;
import com.example.exceldemo.liteflowComponent.common.GenerateDocuments;
import com.example.exceldemo.model.*;
import com.example.exceldemo.util.WordUtil;
import com.yomahub.liteflow.annotation.LiteflowComponent;
import lombok.extern.slf4j.Slf4j;

import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 生成需求测试文档
 * @author yangwb8
 * @Date 2025/5/17 04:06
 */
@LiteflowComponent("two")
@Slf4j
public class GenerateTwoDocuments extends GenerateDocuments {
    @Override
    public void processReqDocuments(ReqInfoDTO reqInfo, String templateDir, String reqOutputDir, Map<String, String> settings) {
        log.info("开始生成需求开发自测报告");
        // 复制基本属性到自测报告对象
        DemoContext deoContext = getDemoContext();
        ReqDevSelfTestReportDTO reqDevSelfTestReportDTO = prepareReqAnalysisData(reqInfo);

        // 将DTO对象转换为Map（POI-TL模板引擎需要Map格式数据）
        Map<String, Object> data = BeanUtil.beanToMap(reqDevSelfTestReportDTO);

        // 创建自测报告专用配置（添加代码高亮渲染策略）
        Configure config = Configure.builder()
                .bind("functionList", getPolicy())
                .bind("terms", getPolicy())
                .bind("testCases", getPolicy())
                .bind("scriptList", new HighlightRenderPolicy())
                .build();

        // 构建输出文件路径
        Path outputPath = Paths.get(reqOutputDir,
                getDocName(reqInfo));

        // 修改：直接使用模板文件名，无需构建完整路径
        String templateFileName = ReqMgrConstants.TEMPLATE_SELF_TEST;
        String provCode = reqInfo.getProvCode();
        
        // 统一使用类路径方式生成文档
        WordUtil.generateWordFileByOS(provCode,templateFileName, outputPath.toString(), data, config);
        log.info("需求开发自测报告 生成成功: {}", outputPath.toString());
    }

    private ReqDevSelfTestReportDTO prepareReqAnalysisData(ReqInfoDTO reqInfo) {
        DemoContext deoContext = getDemoContext();
        ReqDevSelfTestReportDTO reqDevSelfTestReportDTO = BeanUtil.copyProperties(deoContext.getReqAnalysisDTO(), ReqDevSelfTestReportDTO.class);
        // 生成测试用例
        List<TestCaceDTO> testCaseList = generateTestCases(deoContext.getReqAnalysisDTO());
        reqDevSelfTestReportDTO.setRisk(reqInfo.getRisk());
        reqDevSelfTestReportDTO.setTestCases(testCaseList);
        return reqDevSelfTestReportDTO;
    }

    @Override
    public String getDocName(ReqInfoDTO reqInfo) {
        return reqInfo.getReqNo() + "(甘肃)-" + reqInfo.getReqName() + "的测试文档.docx";
    }

    /**
     * 根据功能列表生成测试用例
     *
     * @param reqAnalysisDTO 需求分析数据
     * @return 测试用例列表
     */
    private static List<TestCaceDTO> generateTestCases(ReqAnalysisSpecificationDTO reqAnalysisDTO) {
        List<TestCaceDTO> testCaseList = new ArrayList<>();

        reqAnalysisDTO.getFunctionList().forEach(functionInfo -> {
            TestCaceDTO testCaseDTO = new TestCaceDTO();
            testCaseDTO.setCaseId(RandomUtil.randomLong());
            testCaseDTO.setCaseType("功能测试-" + functionInfo.getName());
            testCaseDTO.setCaseName(functionInfo.getName());
            testCaseDTO.setCaseLevel("1");
            testCaseDTO.setDescription(functionInfo.getDesc());
            testCaseDTO.setExpectedResult("正常");
            testCaseDTO.setInParam("入参-" + functionInfo.getName());
            testCaseDTO.setOutParam("出参-" + functionInfo.getName());
            testCaseDTO.setStatus("测试通过✅");
            testCaseDTO.setCaseOwner(functionInfo.getDeveloper());

            // 生成测试用例编号：需求编号-TEST-随机字符串
            testCaseDTO.setCaseNo(reqAnalysisDTO.getReqNo() + ReqMgrConstants.TEST_PREFIX + RandomUtil.randomString(3));

            testCaseList.add(testCaseDTO);
        });

        return testCaseList;
    }
}
